<?php

namespace app\common\model;

use think\Exception;
use think\Model;

/**
 * 会员模型
 */
class User Extends Model
{
//    protected $hidden=['username','password'];
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'url',
    ];

    /**
     * 获取个人URL
     * @param   string  $value
     * @param   array   $data
     * @return string
     */
    public function getUrlAttr($value, $data)
    {
        return "/u/" . $data['id'];
    }

    /**
     * 获取头像
     * @param   string    $value
     * @param   array     $data
     * @return string
     */
    public function getAvatarAttr($value, $data)
    {
        return $value ? $value : '/assets/img/avatar.png';
    }

    /**
     * 获取会员的组别
     */
    public function getGroupAttr($value, $data)
    {
        return UserGroup::get($data['group_id']);
    }

    /**
     * 获取验证字段数组值
     * @param   string    $value
     * @param   array     $data
     * @return  object
     */
    public function getVerificationAttr($value, $data)
    {
        $value = array_filter((array) json_decode($value, TRUE));
        $value = array_merge(['email' => 0, 'mobile' => 0], $value);
        return (object) $value;
    }

    /**
     * 设置验证字段
     * @param mixed $value
     * @return string
     */
    public function setVerificationAttr($value)
    {
        $value = is_object($value) || is_array($value) ? json_encode($value) : $value;
        return $value;
    }

    /**
     * 变更会员余额
     * @param int $money    余额
     * @param int $user_id  会员ID
     * @param string $memo  备注
     */
    public static function money($money, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user)
        {
            $before = $user->money;
            $after = $user->money + $money;
            //更新会员信息
            $user->save(['money' => $after]);
            //写入日志
            MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        }
    }

    /**
     * 变更会员积分
     * @param int $score    积分
     * @param int $user_id  会员ID
     * @param string $memo  备注
     */
    public static function score($score, $user_id, $memo)
    {
        $user = self::get($user_id);
        if ($user)
        {
            $before = $user->score;
            $after = $user->score + $score;
            $level = self::nextlevel($after);
            //更新会员信息
            $user->save(['score' => $after, 'level' => $level]);
            //写入日志
            ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
        }
    }

    /**
     * 根据积分获取等级
     * @param int $score 积分
     * @return int
     */
    public static function nextlevel($score = 0)
    {
        $lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
        $level = 1;
        foreach ($lv as $key => $value)
        {
            if ($score >= $value)
            {
                $level = $key;
            }
        }
        return $level;
    }

    /**
     * @param $live
     * @param $uid
     * @return bool
     * 我的团队
     */
    public function myteamusers($live,$uid)
    {
        if ($live == 1){
            $list = $this->where('parent_id',$uid)->field('id,username,createtime')->order('id desc')->paginate(10);
            return $list;
        }elseif ($live == 2){
            $uids = $this->where('parent_id',$uid)->column('id');
            if(!$uids) return [];
            $list = $this->where('parent_id','in',$uids)->field('id,username,createtime')->order('id desc')->paginate(10);
            return $list;
        }else{
            return false;
        }
    }
    /**
     * @param $live
     * @param $uid
     * @return bool
     * 我的团队--新的
     */
    public function myteamusersnews($live,$uid,$page,$limit=10)
    {
        try{
            if ($live == 1){
                $list['my']=(object)[];
                $data = $this
                    ->where('parent_id',$uid)
                    ->field('id,username,createtime')
                    ->order('id desc')
                    ->limit($limit)
                    ->page($page)
                    ->select();
                foreach ($data as $key=>$val){
                    $data[$key]['createtime']=date('Y-m-d H:i:s',$val['createtime']);
                }
                $count = $this
                    ->where('parent_id',$uid)
                    ->field('id,username,createtime')
                    ->count();
                $list['count']=$count;
                if(!empty($data)){
                    $list['data']=collection($data)->toArray();
                }else{
                    $list['data']=[];
                }
            }elseif ($live == 2){
                $mylist = $this
                    ->where('id',$uid)
                    ->field('id,username,createtime')
                    ->find();

                $list['my']=$mylist->toArray();

                $data= $this
                ->where('parent_id','in',$uid)
                ->field('id,username,createtime')
                ->order('id desc')
                ->limit($limit)
                ->page($page)
                ->select();
                foreach ($data as $key=>$val){
                    $data[$key]['createtime']=date('Y-m-d H:i:s',$val['createtime']);
                }
                $count= $this
                    ->where('parent_id','in',$uid)
                    ->field('id,username,createtime')
                    ->count();
                $list['count']=$count;
                if(!empty($data)){
                    $list['data']=collection($data)->toArray();
                }else{
                    $list['data']=[];
                }

            }else{
                $list['my']=[];
                $list['data'] =[];
            }
        }catch (Exception $e){
            $list['my']=[];
            $list['data'] =[];
        }
        return $list;

    }

    /**
     * @param $uid
     * 会员加入代理
     */
    public function join_vip($uid)
    {
        return $this->where('id',$uid)->update(['is_vip'=>1]);

    }

    /**1.获取我的积分
     * 2.获取我的余额
     * 3.获取我的贡献值余额
     */
    public  function getMyIntegral($uid,$type){
        switch ($type){
            case 1:
                $sum = 'integral';
                break;
            case 2:
                $sum = 'money';
                break;
            case 3:
                $sum = 'residual';
                break;
            default:
                $sum = 'money';
                break;
        }

        $where['id']=$uid;
        $model = new self();
        return $model->where($where)->sum($sum);
    }






}
